package com.kgc.sbt.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kgc.sbt.api.OrderApi;
import com.kgc.sbt.api.OrderInfoApi;
import com.kgc.sbt.api.UserApi;
import com.kgc.sbt.api.VipUserApi;
import com.kgc.sbt.config.annotation.RequestPermission;
import com.kgc.sbt.constant.ActiveMqConstant;
import com.kgc.sbt.domain.Sign;
import com.kgc.sbt.util.HttpUtils;
import com.kgc.sbt.util.RedisUtils;
import com.kgc.sbt.util.RequestResult;
import com.kgc.sbt.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


/**
 * @description:
 * @author: Andy
 * @time: 2021/6/23 19:41
 */
@RestController
@Slf4j
@Api(tags = "用户积分签到接口，版本号：1.0.0")
@RequestPermission
public class VipUserController {
    @Reference
    private VipUserApi vipUserApi;
    @Autowired
    private RedisUtils redisUtils;


//    private static Jedis jedis = new Jedis("127.0.0.1", 6379);
    /**
     * @param:
     * @return:
     * @auther: gx
     * @date: 2021/6/23 0022 14.233
     * 功能描述: 增加积分，输入用户id和所增加的积分
     */
    @GetMapping("/addScore")
    @ApiOperation(value = "增加用户积分", httpMethod = "GET",notes = "内部接口，支付完成调用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, value = "用户id"),
            @ApiImplicitParam(name = "score", required = true, value = "积分")
    })
    public RequestResult<String> addScore(@RequestParam Integer userId, @RequestParam Integer score){
        if(vipUserApi.queryUserById(userId)){
            if(vipUserApi.addScore(userId,score)){
                log.info("当前积分为:"+ vipUserApi.queryScore(userId));
                return  ResultBuildUtil.success("成功;当前积分为:"+ vipUserApi.queryScore(userId));
            }else{
                log.info("当前积分为:"+ vipUserApi.queryScore(userId));
                System.out.println("当前积分为:"+ vipUserApi.queryScore(userId));
                return ResultBuildUtil.fail( "3301","失败");
            }
        }
        return ResultBuildUtil.fail( "3301","失败,没有此用户id");

    }

    /**
     * @param:
     * @return:
     * @auther: gx
     * @date: 2021/6/23 0022 14.233
     * 功能描述: 减少积分，输入用户id和所增加的积分
     */
    @GetMapping("/reduceScore")
    @ApiOperation(value = "减少用户积分", httpMethod = "GET",notes = "内部接口，领取消费券调用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, value = "用户id"),
            @ApiImplicitParam(name = "score", required = true, value = "积分")
    })
    public RequestResult<String> reduceScore(@RequestParam Integer userId, @RequestParam Integer score){
        if(vipUserApi.queryUserById(userId)){
            if(vipUserApi.reduceScore(userId,score)){
                log.info("当前积分为:"+ vipUserApi.queryScore(userId));
                return  ResultBuildUtil.success("成功;当前积分为:"+ vipUserApi.queryScore(userId));
            }else{
                log.info("当前积分为:"+ vipUserApi.queryScore(userId));
                System.out.println("当前积分为:"+ vipUserApi.queryScore(userId));
                return ResultBuildUtil.fail( "3301","失败");
            }
        }
        return ResultBuildUtil.fail( "3301","失败,没有此用户id");
    }

    @GetMapping("/sign")
    @ApiOperation(value = "用户签到", httpMethod = "GET")
    public RequestResult<String> UserSign(HttpServletRequest request){
        String token = request.getHeader("token");
        // 校验token是否有效
        Integer userId = (Integer) redisUtils.get(token);
        log.info("------ userId:" + userId);
        if(vipUserApi.userSigned(userId)==1) {
            Sign sign = vipUserApi.querySignByUserId(userId);
            Integer countSign = sign.getContinueSign();
            vipUserApi.addScore(userId, 1);
            Integer score = vipUserApi.queryScore(userId);
            if (countSign == 0) {
                return ResultBuildUtil.success("当前用户" + userId + "签到成功,已连续签到7天获得额外20积分,当前积分" + score);
            } else {
                return ResultBuildUtil.success("当前用户" + userId + "签到成功,积分增加1，,当前积分" + score);
            }
        }else if(vipUserApi.userSigned(userId)==2){
            return ResultBuildUtil.success("今天已经签到完成，明天再来吧");
        }else{
            return ResultBuildUtil.fail("2201","签到失败");
        }

    }








}
 